﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using QuanLyThuVien.Model;
using QuanLyThuVien.Core;

namespace QuanLyThuVien.DataAccess
{
    class DocGiaAccess
    {
        dbConnection db = new dbConnection();


        public DataTable layToanboTTDocgia()
        {
            return db.executeSelectProcedureQueryNonParameter("select_docgia");

        }
        //---- Tìm kiếm theo mã độc giả
        public DataView timkiemMadg(string Madg)
        {
            DataView dataView = layToanboTTDocgia().DefaultView;
            dataView.RowFilter = string.Format("Ma_the like '%{0}%'", Madg);
            return dataView;
        }
        //---- Tìm kiếm theo tên độc giả
        public DataView timkiemTendg(string Tendg)
        {
            DataView dataView = layToanboTTDocgia().DefaultView;
            dataView.RowFilter = string.Format("Ten_docgia like '%{0}%'", Tendg);
            return dataView;
        }
        //---- Tim Học sinh theo ma va tra ve mot bang
        public DataTable timdocgiatheoma(string madg)
        {
            SqlParameter[] listParam = new SqlParameter[1];
            SqlParameter ma = new SqlParameter("@Ma_the", SqlDbType.Char, 10);
            ma.Value = madg;
            listParam[0] = ma;
            return db.executeSelectProcedureQuery("timkiem_docgia", listParam);
        }
        //---- Them doc gia
        public int themDocGia(DocGia dg)
        {

            if ((timdocgiatheoma(dg.Ma_the1).Rows.Count>0))
                return 0;
            else
            {
                SqlParameter[] listParam = new SqlParameter[6];
                SqlParameter mathe = new SqlParameter("@Ma_the", SqlDbType.Char, 10);
                mathe.Value = dg.Ma_the1;
                listParam[0] = mathe;
                listParam[1] = new SqlParameter("@Ten_docgia", dg.Ten_docgia1);
                listParam[2] = new SqlParameter("@Ngay_sinh", string.Format("{0:yyyy-MM-dd}", dg.Ngay_sinh1));
                listParam[3] = new SqlParameter("@Gioi_tinh", dg.Gioi_tinh1);
                listParam[4] = new SqlParameter("@Dia_chi", dg.Dia_chi1);
                listParam[5] = new SqlParameter("@Dien_thoai", dg.Dien_thoai1);
                db.executeInsertProcedureQuery("intsert_docgia", listParam);
                return 1;
            }
        }
        //---- Sửa độc giả
        public int suaDocGia(DocGia dg)
        {
            if ((timdocgiatheoma(dg.Ma_the1) == null))
                return 0;
            else
            {
                SqlParameter[] listParam = new SqlParameter[6];
                SqlParameter mathe = new SqlParameter("@Ma_the", SqlDbType.Char, 10);
                mathe.Value = dg.Ma_the1;
                listParam[0] = mathe;
                listParam[1] = new SqlParameter("@Ten_docgia", dg.Ten_docgia1);
                listParam[2] = new SqlParameter("@Ngay_sinh", string.Format("{0:yyyy-MM-dd}", dg.Ngay_sinh1));
                listParam[3] = new SqlParameter("@Gioi_tinh", dg.Gioi_tinh1);
                listParam[4] = new SqlParameter("@Dia_chi", dg.Dia_chi1);
                listParam[5] = new SqlParameter("@Dien_thoai", dg.Dien_thoai1);
                db.executeUpdateProcedureQuery("update_docgia", listParam);
                return 1;
            }
        }
        // Xóa độc giả
        public int xoadg(DocGia dg)
        {

            if ((timdocgiatheoma(dg.Ma_the1) == null))
                return 0;
            else
            {
                SqlParameter[] listParam = new SqlParameter[1];
           
                SqlParameter mathe = new SqlParameter("@Ma_the", SqlDbType.Char, 10);
                mathe.Value = dg.Ma_the1;
                listParam[0] = mathe;
                db.executeDeleteProcedureQuery("delete_docgia", listParam);
                return 1;
            }
        }
    }
}


